package org.scboy.rbac.controller;

import java.util.List;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.UserAccountService;
import org.scboy.rbac.service.UserOrgService;
import org.scboy.rbac.service.UserResourceService;
import org.scboy.rbac.service.UserService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.reflect.TypeToken;

import cn.hutool.core.util.StrUtil;

@RestController
@RequestMapping("rbac/user")
@Menu("权限管理系统.用户管理")
public class UserController {

	@Autowired
	UserService userService;
	
	@Autowired
	UserResourceService userResourceService;
	
	@Autowired
	UserOrgService userOrgService;
	
	@Autowired
	UserAccountService userAccountService;
	
	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Permission("分页查询用户")
	public Result page(@RequestBody Model params){
		Page page=userService.page(params);
		return Result.Success(page);
	}
	
	/**
	 * 新增用户
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增用户")
	public Result add(@RequestBody Model params){
		if(StrUtil.isNotEmpty(params.getString("loginAccount"))){
			if(userAccountService.getAccountByLoginName(params.getString("loginAccount"))!=null){
				return Result.Error("RBAC_USER_ACCOUNT_CHECK");
			}
		}
		if(userService.add(params)){
			return Result.Success("RBAC_USER_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_ADD_ERROR");
		}
	}
	
	/**
	 * 获取用户信息
	 * @param request
	 * @param id 
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取用户信息")
	public Result get(String id){
		return Result.Success(userService.get(id),"");	
	}
	/**
	 * 获取用户信息
	 * @param request
	 * @param id 
	 * @return
	 */
	@GetMapping("getUserInfo")
	@Permission("获取用户详细信息")
	public Result getUserInfo(String id){
		return Result.Success(userService.getUserInfo(id),"");	
	}
	
	/**
	 * 更新用户
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新用户")
	public Result update(@RequestBody Model params){
		if(userService.update(params)){
			return Result.Success("RBAC_USER_UPDATE_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_UPDATE_ERROR");
		}	
	}
	/**
	 * 导入用户
	 * @param request
	 * @return
	 */
	@PostMapping("import")
	@Permission("导入用户")
	public Result importUser(String datas){
		List<Model> lists=new Gson().fromJson(datas, new TypeToken<List<Model>>(){}.getType());
		String msg=userService.importUser(lists);
		if(StrUtil.isEmpty(msg)){
			return Result.Success("RBAC_USER_IMPORT_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_IMPORT_ERROR").msgMapping(Model.create().add("msg", msg));
		}
	}
	
	/**
	 * 启用禁用
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用")
	public Result checkState(String id){
		if(userService.checkState(id)){
			return Result.Success("RBAC_USER_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_CHECKSTATE_ERROR");
		}	
	}
	
	/**
	 * 重置密码
	 * @param request
	 * @param newPassword
	 * @param oldPassword
	 * @return
	 */
	@PutMapping("repassword")
	@Permission("重置密码")
	public Result rePassword(@RequestBody Model params){
		String userId = params.getString("userId");
		String newPassword = params.getString("newPassword");
		String oldPassword = params.getString("oldPassword");
		String reNewPassword = params.getString("reNewPassword");
		if (newPassword.equals(reNewPassword)) {
			if(userService.get(userId, oldPassword)==null){
				return Result.Error("RBAC_USER_CHECK_PASSWORD_ERROR");
			}
			if(userService.rePassword(userId, newPassword)){
				return Result.Success("RBAC_USER_REPASSWORD_SUCCESS");
			}else{
				return Result.Error("RBAC_USER_REPASSWORD_ERROR");
			}
		} else {
			return Result.Error("RBAC_USER_PASSWORD_DIFFERENT");
		}
		
	}
	
	/**
	 * 管理员重置密码
	 * @param request
	 * @param password
	 * @return
	 */
	@PutMapping("adminrepassword")
	@Permission("管理员重置密码")
	public Result adminRepassword(@RequestBody Model params){
		String id = params.getString("id");
		String newPassword = params.getString("newPassword");
		String reNewPassword = params.getString("reNewPassword");
		if (newPassword.equals(reNewPassword)) {
			if (userService.rePassword(id, newPassword)) {
				return Result.Success("RBAC_USER_REPASSWORD_SUCCESS");
			} else {
				return Result.Error("RBAC_USER_REPASSWORD_ERROR");
			}
		} else {
			return Result.Error("RBAC_USER_PASSWORD_DIFFERENT");
		}
	}
	
	/**
	 * 移动用户组织
	 * @param request
	 * @param userId
	 * @param orgId
	 * @return
	 */
	@PutMapping("move")
	@Permission("移动用户组织")
	public Result move(String userId,String orgId){
		if(userOrgService.move(userId, orgId)){
			return Result.Success("RBAC_USER_ORG_MOVE_SUCCES");
		}else{
			return Result.Error("RBAC_USER_ORG_MOVE_ERROR");
		}
	}
	
	/**
	 * 新增用户资源关系
	 * @param request
	 * @param userId
	 * @param roleId
	 * @return
	 */
	@PostMapping("adduserres")
	@Permission("新增用户资源关系")
	public Result addUserRole(String userId,String resId){
		if(userResourceService.add(userId, resId)){
			return Result.Success("RBAC_USER_ROLE_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_ROLE_CHECK");
		}
	}
	
	/**
	 * 移除用户资源关系
	 * @param request
	 * @param id
	 * @return
	 */
	 @DeleteMapping("removeuserres")
	 @Permission("移除用户资源关系")
	 public Result removeUserRes(String id){
		 if(userResourceService.delete(id)){
			return Result.Success("RBAC_USER_ROLE_REMOVE_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_ROLE_REMOVE_ERROR");
		}
	 }
	 
	 /**
	  * 分页查询用户关联角色
	  * @param request
	  * @param id
	  * @return
	  */
	 @PostMapping("getress")
	 @Permission("分页查询用户关联资源")
	 public Result getRess(@RequestBody Model params){
		 Page page=userResourceService.page(params);
		return Result.Success(page);
	 }
	 
}
